package net.suncaper.demo.controller;

import net.suncaper.demo.domain.Information;
import net.suncaper.demo.service.InformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Controller
@RequestMapping("/information")
public class Information_controller {
    @Autowired
    private InformationService informationService;
    private String UserId;
    private int select;

    @GetMapping("/add")
    public String addPage( Model model,Model mode0) {
//        model.addAttribute("information", new Information());
        mode0.addAttribute("UserId",UserId);
        return "/pages/information/write_information.html";
    }
    @PostMapping("/add")
    public String saveInformation(Information information) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        information.setDate(df.format(new Date()));
        information.setAdministratorId(UserId);
        informationService.saveInformation(information);
        return "redirect:/information";
    }
    @GetMapping("update")
    public String updateInformation(Model model,Model mode0,@RequestParam(value = "name", required = false) BigDecimal InformationId) {
        Information information = informationService.findInformationById(InformationId);
        model.addAttribute("information", information);
        mode0.addAttribute("UserId",UserId);
        return "/pages/information/updateinformation.html";
    }
    @PostMapping("/update")
    public String UPdateInformation(Information information,@RequestParam(value = "name", required = false) BigDecimal InformationId) {

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        information.setDate(df.format(new Date()));
        information.setAdministratorId(UserId);
        information.setInformationId(InformationId);
        informationService.updateInformation(information);
        return "redirect:/information/Manager";
    }
    @RequestMapping("/delete")
    public String deleteInformation(Model model,@RequestParam(value = "name", required = false) BigDecimal InformationId) {
        informationService.deleteInformationById(InformationId);
        return "redirect:/information/Manager";
    }
    @RequestMapping("/Manager")
    public String ManagerInformation(Model model,Model mode0) {
        List<Information> information = informationService.findInformation();
        model.addAttribute("information",information);
        mode0.addAttribute("UserId",UserId);
        return "pages/information/manage_information.html";

    }
    @RequestMapping("/Managertitle")
    public String ManagerInformation(Model model,Model mode0,@RequestParam(value = "title", required = false) String title) {
        List<Information> information1 = informationService.findInformation();
        List<Information> information2 = new ArrayList<Information>();
        for(int i=0;i<information1.size();i++){
            if(information1.get(i).getTitle().indexOf(title) != -1) {
                information2.add(information1.get(i));
            }
        }
        model.addAttribute("information", information2);
        mode0.addAttribute("UserId",UserId);
        return "pages/information/manage_information.html";

    }

    @RequestMapping("/search")
    public String searchInformation(Model model,Model mode0,@RequestParam(value = "name", required = false) BigDecimal InformationId) {
        Information information = informationService.findInformationById(InformationId);
        model.addAttribute("information", information);
        mode0.addAttribute("UserId",UserId);
        if(select==1) {
            return "pages/information/ShowInformation_Admin.html";
        }
        else if(select==2){
            return "pages/information/ShowInformation_Teacher.html";
        }
        else{
            return "pages/information/ShowInformation_Student.html";
        }
    }
    @RequestMapping("/set")
    public String SetAdministratorId(@RequestParam(value = "UserId", required = false) String UserId,@RequestParam(value = "select", required = false) int select) {
        this.UserId=UserId;
        this.select=select;
        if(select==1){
            return "redirect:/question/set?UserId="+UserId+"&select=1";}
        if(select==2){
            return "redirect:/question/set?UserId="+UserId+"&select=2";}
        else{
            return "redirect:/question/set?UserId="+UserId+"&select=3";}
    }
    @RequestMapping("/title")
    public String userPage1(Model model,Model mode0,@RequestParam(value = "title", required = false) String title) {
        List<Information> information1 = informationService.findInformation();
        List<Information> information2 = new ArrayList<Information>();
        for(int i=0;i<information1.size();i++){
            if(information1.get(i).getTitle().indexOf(title) != -1) {
                information2.add(information1.get(i));
            }
        }
        model.addAttribute("information", information2);
        mode0.addAttribute("UserId",UserId);
        if(select==1) {
            return "pages/menu/AdministratorMenu.html";
        }
        else if(select==2){
            return "pages/menu/TeacherMenu.html";
        }
        else{
            return "pages/menu/StudentMenu.html";
        }
    }
    @RequestMapping
    public String AdministratorPage(Model model,Model mode0) {
        List<Information> information = informationService.findInformation();
        model.addAttribute("information",information);
        mode0.addAttribute("UserId",UserId);
        if(select==1) {
            return "pages/menu/AdministratorMenu.html";
        }
        else if(select==2){
            return "pages/menu/TeacherMenu.html";
        }
        else{
            return "pages/menu/StudentMenu.html";
        }
    }
}
